<?php

namespace App\Util;

use App\Component\UserIdentity\BackendUser;
use App\Core\IdentityInterface;
use EasySwoole\Component\Di;

class UserIdentityHelper
{
    
    public static function getIdentity()
    {
        /** @var IdentityInterface $userIdentity */
        $userIdentity = Di::getInstance()->get('user') ? Di::getInstance()->get('user')->getIdentity() : null;
        return $userIdentity;
    }
    
    public static function getUsername()
    {
        /** @var IdentityInterface $userIdentity */
        $userIdentity = Di::getInstance()->get('user') ? Di::getInstance()->get('user')->getIdentity() : null;
        return $userIdentity ? $userIdentity->getUsername() : '';
    }
    
    public static function getId()
    {
        /** @var IdentityInterface $userIdentity */
        $userIdentity = Di::getInstance()->get('user') ? Di::getInstance()->get('user')->getIdentity() : null;
        return $userIdentity ? $userIdentity->getId() : 0;
    }

    public static function getType()
    {
        $userIdentity = Di::getInstance()->get('user') ? Di::getInstance()->get('user')->getIdentity() : null;
        return $userIdentity ? $userIdentity->getType() : 0;
    }

    public static function validatePassword($password)
    {
        $userIdentity = Di::getInstance()->get('user') ? Di::getInstance()->get('user')->getIdentity() : null;
        return $userIdentity ? ($userIdentity->getPassword() == CommonHelper::pwdEncrypt($password, $userIdentity->getSalt())) : false;
    }
}